<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .progress {
        border: solid;
        padding: 5px;
        margin: 10px;
      }
      .progress .bar {
        height: 10px;
        width: 0%;
        background: greenyellow;
      }
    </style>
  </head>
  <body>
    <h1>HELLO Index</h1>

    <div id="errorBox"></div>
    <!--  -->
    <div class="progress" id="progress">
      <div class="bar" id="progressBar"></div>
    </div>
    <!--  -->
    <video id="videoPlay" style="width: 500px; display: none" controls></video>

    <script>
      const baseURL = "http://127.0.0.1:8090/api";

      const xhr = new XMLHttpRequest();
      // xhr.readyState
      console.log("初始状态", xhr.readyState); //0
      xhr.open(
        "GET",
        `${baseURL}/files/7atixn7gmw5feoq/mvr6qbz33642v4v/12_10_2gtMtBCkTE.mp4`
      );
      console.log("open 被调用", xhr.readyState); //1

      // 下载进度
      xhr.onprogress = function (e) {
        progressBar.style.width = (e.loaded / e.total) * 100 + "%";
        console.log("响应正在被加载", xhr.readyState); //3
      };

      /* response */
      xhr.responseType = "blob";

      // 成功响应 200 ok
      xhr.onload = function () {
        console.log("请求完成", xhr.readyState); //3
        // 获取状态
        console.log(xhr.status);
        console.log(xhr.statusText);
        // body
        videoPlay.src = URL.createObjectURL(xhr.response);
        videoPlay.style.display = "";
        progress.style.display = "none";
      };

      /* xhr.readyState */
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 2) {
          console.log("接收到 response header");
        }
        if (xhr.readyState == 3) {
          console.log("响应正在被加载");
        }
        if (xhr.readyState == 4) {
          console.log("请求完成");
        }
      };
      /* request */
      xhr.send();
    </script>
  </body>
</html>
